{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# genHtml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fileName:test.txt\n"
     ]
    }
   ],
   "source": [
    "fileName = input(\"fileName:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "modelName:model\n"
     ]
    }
   ],
   "source": [
    "modelName = input(\"modelName:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Input = \"1\"\n",
    "Select = \"2\"\n",
    "Date = \"3\"\n",
    "Money = \"4\"\n",
    "Rate = \"5\"\n",
    "Area = \"6\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "class HtmlTmpl:\n",
    "    def __init__(self):\n",
    "        self.headTmpl = \"\"\"<head>\"\"\"\n",
    "        self.footTmpl = \"\"\"<foot>\"\"\"\n",
    "        self.inputTmpl = \"\"\"\n",
    "<input id=\"{0}\" name=\"{1}\" value=\"${2}\" \n",
    "  class=\"{3}\"></input>\n",
    "\"\"\"\n",
    "        self.selectTmpl = \"\"\"<select id=\"{0}\" name=\"{1}\" value=\"${2}\" class=\"{3}\"></select>\"\"\"\n",
    "        self.dateTmpl = \"\"\"<date id=\"{0}\" name=\"{1}\" value=\"${2}\" class=\"{3}\"></dateS>\"\"\"\n",
    "        self.moneyTmpl = \"\"\"<money id=\"{0}\" name=\"{1}\" value=\"${2}\" class=\"{3}\"></money>\"\"\"\n",
    "        self.rateTmpl = \"\"\"<rate id=\"{0}\" name=\"{1}\" value=\"${2}\" class=\"{3}\"></rate>\"\"\"\n",
    "        self.areaTmpl = \"\"\"<area id=\"{0}\" name=\"{1}\" value=\"${2}\" class=\"{3}\"></area>\"\"\"\n",
    "    #model为实体类名字 传入的arr格式为 [propName,propType,required]\n",
    "    def genTmpl(self,model,arr):\n",
    "        if len(arr) != 3:\n",
    "            print(\"输入文件格式错误!\")\n",
    "            return -1\n",
    "        propName = arr[0]\n",
    "        propType = arr[1]\n",
    "        required = arr[2]\n",
    "        \n",
    "        id = model + \"_\" + propName #id = model_propName\n",
    "        name = model + \".\" + propName #name = model.propName\n",
    "        value = \"{\" + model + \".\" + propName + \"}\" #value = {model.propName}\n",
    "        if propType == Input:\n",
    "            if required == \"0\":\n",
    "                return self.inputTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.inputTmpl.format(id,name,value,\"required\")\n",
    "        elif propType == Select:\n",
    "            if required == \"0\":\n",
    "                return self.selectTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.selectTmpl.format(id,name,value,\"required\")\n",
    "        elif propType == Date:\n",
    "            if required == \"0\":\n",
    "                return self.dateTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.dateTmpl.format(id,name,value ,\"required\")\n",
    "        elif propType == Money:\n",
    "            if required == \"0\":\n",
    "                return self.moneyTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.moneyTmpl.format(id,name,value,\"required\")\n",
    "        elif propType == Rate:\n",
    "            if required == \"0\":\n",
    "                return self.rateTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.rateTmpl.format(id,name,value,\"required\")\n",
    "        elif propType == Area:\n",
    "            if required == \"0\":\n",
    "                return self.areaTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.areaTmpl.format(id,name,value,\"required\")\n",
    "        else:\n",
    "            print(\"Unknown Type!\")\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1 = open(fileName,\"r\") #input file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "f2 = open(modelName,\"a+\") #output file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmpl = HtmlTmpl()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f2.write(tmpl.headTmpl + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "for line in f1:\n",
    "    f2.write(tmpl.genTmpl(modelName,line.split()) + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f2.write(tmpl.footTmpl + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "f2.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
